package net.mingsoft.basic.strategy;

import net.mingsoft.basic.biz.*;
import org.springframework.beans.factory.annotation.*;
import net.mingsoft.basic.entity.*;
import net.mingsoft.basic.util.*;
import org.apache.commons.lang3.*;
import net.mingsoft.base.entity.*;
import cn.hutool.crypto.*;
import org.apache.shiro.*;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.*;

public class ManagerLoginStrategy implements ILoginStrategy
{
    @Autowired
    private IManagerBiz managerBiz;
    
    @Override
    public Boolean login(final ManagerEntity manager) {
        this.managerBiz.updateCache();
        final boolean rememberMe = BasicUtil.getBoolean("rememberMe");
        if (manager == null || StringUtils.isEmpty((CharSequence)manager.getManagerName()) || StringUtils.isEmpty((CharSequence)manager.getManagerPassword())) {
            return false;
        }
        final ManagerEntity newManager = new ManagerEntity();
        newManager.setManagerName(manager.getManagerName());
        final ManagerEntity _manager = (ManagerEntity)this.managerBiz.getEntity((BaseEntity)newManager);
        if (_manager == null) {
            return false;
        }
        if (SecureUtil.md5(manager.getManagerPassword()).equals(_manager.getManagerPassword())) {
            final Subject subject = SecurityUtils.getSubject();
            final UsernamePasswordToken upt = new UsernamePasswordToken(_manager.getManagerName(), _manager.getManagerPassword());
            upt.setRememberMe(rememberMe);
            subject.login((AuthenticationToken)upt);
            return true;
        }
        return false;
    }
}
